
package robotinterface.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import robotinterface.list.entry.Entry;
import robotinterface.util.DBManager;

/**
 *
 * @author Bailey
 */
public class EntryDAO {
    
    static private Connection connection;

    public EntryDAO() {
        connection = DBManager.getConnection();
    }
      
    static public void connect() {
        connection = DBManager.getConnection();
    }
        
    static public void disconnect() {
        if (connection != null) {
            try { connection.close(); } 
            catch (SQLException ignore) { }
        }
    }
     
    static public boolean addLog(Entry iEntry) {
        try {
            String query = "INSERT INTO Log VALUES(?,?)";
            PreparedStatement ptmt = connection.prepareStatement(query);

            ptmt.setString(1, iEntry.getTitle());
            ptmt.setString(2, iEntry.getToolTip());

            ptmt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        }
        return false;
    }
    
    static public List listUsers() {
        List iList = new ArrayList();
        try {
            Entry uEntry;
            String querystring = "SELECT * FROM Entry";

            PreparedStatement ptmt = connection.prepareStatement(querystring);
            ResultSet rs = ptmt.executeQuery();
            while (rs.next()) {
                uEntry = new Entry(rs.getString(1));
                iList.add(uEntry);
            }
        } catch (SQLException ex) {
            System.out.println( ex.getMessage() );
        }
        return iList;
    }
}
